
#全局变量

KERNEL_DIR=/home/pi/linux/linux-5.4.99

SDK_DIR=/home/pi/f1c200s_linux_sdk

SDK_DIR_BUILDROOT=/home/pi/f1c200s_linux_sdk/buildroot-overlay

#export ARCH=arm
#export CROSS_COMPILE=/home/pi/f1c200s_linux_sdk/output/my24ok_f1c200s_defconfig/host/bin/arm-none-linux-gnueabi-
#make  f1c200s_defconfig

do_donwload () {
	echo -e "开始下载文件"
cd ~
if [ ! -d "f1c200s_linux_sdk" ] ;then 
	git clone https://gitee.com/dmcus/f1c200s_linux_sdk.git
fi
#获取文件
mkdir -p ~/linux 
cd ~/linux 
if [ ! -f "linux-5.4.99.tar.gz" ] ;then 
	wget -c https://mirrors.ustc.edu.cn/kernel.org/linux/kernel/v5.x/linux-5.4.99.tar.gz
fi
if [ ! -d "linux-5.4.99" ] ;then
	tar -xf linux-5.4.99.tar.gz
fi
echo -e "完成"
}
 


do_patch_files () {
echo -e "开始打补丁"
echo "${KERNEL_DIR}"
echo "${SDK_DIR}"
 
#打补丁
if [ ! -f ".patced" ] ;then
patch -d ${KERNEL_DIR}  -p1 < ${SDK_DIR_BUILDROOT}/board/allwinner/suniv-f1c100s/patch/linux/0001-Add-USB-Support.patch
patch -d ${KERNEL_DIR}  -p1 < ${SDK_DIR_BUILDROOT}/board/allwinner/suniv-f1c100s/patch/linux/0002-Fix-CCU-definition.patch
patch -d ${KERNEL_DIR}  -p1 < ${SDK_DIR_BUILDROOT}/board/allwinner/suniv-f1c100s/patch/linux/0003-Add-DMA-Controller.patch
patch -d ${KERNEL_DIR}  -p1 < ${SDK_DIR_BUILDROOT}/board/allwinner/suniv-f1c100s/patch/linux/0004-Add-Audio-Codec.patch
patch -d ${KERNEL_DIR}  -p1 < ${SDK_DIR_BUILDROOT}/board/allwinner/suniv-f1c100s/patch/linux/0005-Add-sun4i-csi-packed-format.patch
patch -d ${KERNEL_DIR}  -p1 < ${SDK_DIR_BUILDROOT}/board/allwinner/suniv-f1c100s/patch/linux/0006-Add-rfkill-gpio-DT-support.patch
patch -d ${KERNEL_DIR}  -p1 < ${SDK_DIR_BUILDROOT}/board/allwinner/suniv-f1c100s/patch/linux/0007-Fix-spi-nand-BBM-size.patch
patch -d ${KERNEL_DIR}  -p1 < ${SDK_DIR_BUILDROOT}/board/allwinner/suniv-f1c100s/patch/linux/0008-Add-CedarX-Driver.patch
patch -d ${KERNEL_DIR}  -p1 < ${SDK_DIR_BUILDROOT}/board/allwinner/suniv-f1c100s/patch/linux/0009-Fix-ancient-GDF5-A-series-NAND.patch
patch -d ${KERNEL_DIR}  -p1 < ${SDK_DIR_BUILDROOT}/board/allwinner/suniv-f1c100s/patch/linux/0010-Add-AW9523B-GPIO-Expender.patch
patch -d ${KERNEL_DIR}  -p1 < ${SDK_DIR_BUILDROOT}/board/allwinner/suniv-f1c100s/patch/linux/0011-Allow-phy-sun4i-usb-use-nested-irq.patch
patch -d ${KERNEL_DIR}  -p1 < ${SDK_DIR_BUILDROOT}/board/allwinner/suniv-f1c100s/patch/linux/0012-Add-AXP199-Support.patch
patch -d ${KERNEL_DIR}  -p1 < ${SDK_DIR_BUILDROOT}/board/allwinner/suniv-f1c100s/patch/linux/0013-Add-XT25F128-SPI-NOR.patch
patch -d ${KERNEL_DIR}  -p1 < ${SDK_DIR_BUILDROOT}/board/allwinner/suniv-f1c100s/patch/linux/0014-Add-GD5F1GQ5UExxG-SPI-NAND.patch
patch -d ${KERNEL_DIR}  -p1 < ${SDK_DIR_BUILDROOT}/board/allwinner/suniv-f1c100s/patch/linux/0015-Workaround-for-old-W25N01G.patch
patch -d ${KERNEL_DIR}  -p1 < ${SDK_DIR_BUILDROOT}/board/allwinner/suniv-f1c100s/patch/linux/0016-Add-onchip_ecc_quirk-switch-for-spi-nand.patch
touch .patced
fi
echo -e "完成"
}

do_copy_files () {
 echo -e "开始复制文件"
echo "${SDK_DIR_BUILDROOT}"
 #复制dts
cp -f ${SDK_DIR_BUILDROOT}/board/allwinner/suniv-f1c100s/devicetree/linux/suniv-f1c100s.dtsi  ${KERNEL_DIR}/arch/arm/boot/dts/ 
cp -f ${SDK_DIR_BUILDROOT}/board/widora/mangopi/r3/devicetree/linux/devicetree.dts ${KERNEL_DIR}/arch/arm/boot/dts/

#复制linux config
cp -f ${SDK_DIR_BUILDROOT}/board/allwinner/suniv-f1c100s/linux.defconfig ${KERNEL_DIR}/arch/arm/configs
mv ${KERNEL_DIR}/arch/arm/configs/linux.defconfig ${KERNEL_DIR}/arch/arm/configs/f1c200s_defconfig 
echo -e "完成"
}

do_make_kernel () {
echo -e "开始编译内核"
cd ${KERNEL_DIR}
#编译 kernel
export ARCH=arm
export CROSS_COMPILE=${SDK_DIR}/output/my24ok_f1c200s_defconfig/host/bin/arm-none-linux-gnueabi-
make  f1c200s_defconfig
make -j6
echo -e "完成"
}

do_make_dtb () {
echo -e "开始编译 设备树"
#dts预处理 gcc命令
/usr/bin/gcc -O2 -isystem ./include \
-E -Wp,-MD,arch/arm/boot/dts/.devicetree.dtb.d.pre.tmp -nostdinc \
-I./scripts/dtc/include-prefixes \
-undef -D__DTS__ -x assembler-with-cpp \
-o arch/arm/boot/dts/.devicetree.dtb.dts.tmp arch/arm/boot/dts/devicetree.dts

#输出dtb命令
cd ${KERNEL_DIR}
./scripts/dtc/dtc -O dtb -o arch/arm/boot/dts/devicetree.dtb \
-b 0 -iarch/arm/boot/dts/ -i./scripts/dtc/include-prefixes -Wno-unit_address_vs_reg -Wno-unit_address_format \
-Wno-avoid_unnecessary_addr_size -Wno-alias_paths -Wno-graph_child_address -Wno-simple_bus_reg -Wno-unique_unit_address -Wno-pci_device_reg  \
-d arch/arm/boot/dts/.devicetree.dtb.d.dtc.tmp arch/arm/boot/dts/.devicetree.dtb.dts.tmp

#输出debug
cat arch/arm/boot/dts/.devicetree.dtb.d.pre.tmp arch/arm/boot/dts/.devicetree.dtb.d.dtc.tmp > arch/arm/boot/dts/.devicetree.dtb.d
echo -e "完成"
}


set -e
do_donwload
do_patch_files
do_copy_files
do_make_kernel
do_make_dtb
 